package cn.howxu.yichen_ovo.common.item;

import cn.howxu.yichen_ovo.init.ModEffects;
import cn.howxu.yichen_ovo.init.ModSounds;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.network.chat.Component;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @description: TODO
 * @author: HowXu
 * @date: 2025/10/3 11:18
 */
public class WineItem extends BlockItem {
    
    private static final SoundInstance INSTANCE = SimpleSoundInstance.forUI(ModSounds.DAN.get(), 1.0f);
    
    public WineItem(Block pBlock) {
        super(pBlock, new Item.Properties().food((new FoodProperties.Builder()).nutrition(3).saturationMod(0.08F).effect(new MobEffectInstance(ModEffects.YICHEN.get(),320 * 20,5,true,false),100.0f).alwaysEat().build()));
    }
    
    @Override
    public EquipmentSlot getEquipmentSlot(ItemStack stack) {
        return EquipmentSlot.HEAD;
    }

    @Override
    public UseAnim getUseAnimation(ItemStack pStack) {
        // 喝水
        return UseAnim.DRINK;
    }

    @Override
    public int getUseDuration(ItemStack pStack) {
        return 32;
    }

    @Override
    public void onUseTick(Level pLevel, LivingEntity pLivingEntity, ItemStack pStack, int pRemainingUseDuration) {
        var manager = Minecraft.getInstance().getSoundManager();
        super.onUseTick(pLevel, pLivingEntity, pStack, pRemainingUseDuration);
        if (pLevel.isClientSide()){
            if(!manager.isActive(INSTANCE)){
                manager.play(INSTANCE);
            }
            //pLevel.playSound(null,pLivingEntity.getOnPos(), ModSounds.DAN.get(), SoundSource.PLAYERS,1.0f,1.0f);
        }
    }

    @Override
    public void onStopUsing(ItemStack stack, LivingEntity entity, int count) {
        super.onStopUsing(stack, entity, count);
        var manager = Minecraft.getInstance().getSoundManager();
        if (entity.level().isClientSide()){
            if(manager.isActive(INSTANCE)){
                manager.stop(INSTANCE);
            }
            //pLevel.playSound(null,pLivingEntity.getOnPos(), ModSounds.DAN.get(), SoundSource.PLAYERS,1.0f,1.0f);
        }
    }

    @Override
    public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @NotNull List<Component> pTooltip, @NotNull TooltipFlag pFlag) {
        super.appendHoverText(pStack, pLevel, pTooltip, pFlag);
        pTooltip.add(Component.translatable("tip.yichenovo.7+2").withStyle(ChatFormatting.GREEN));
    }
}
